package cn.iocoder.yudao.module.enterprise.controller.admin.organization;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.enterprise.controller.admin.organization.vo.OrganizationCreateReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.organization.vo.OrganizationRespVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.organization.vo.OrganizationUpdateReqVO;
import cn.iocoder.yudao.module.enterprise.convert.organization.OrganizationConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.controlplan.ControlPlanDO;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.organization.OrganizationDO;
import cn.iocoder.yudao.module.enterprise.service.controlplan.ControlPlanService;
import cn.iocoder.yudao.module.enterprise.service.organization.OrganizationService;
import cn.iocoder.yudao.module.system.api.tenant.TenantApi;
import cn.iocoder.yudao.module.system.api.tenant.dto.TenantRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name =  "管理后台 - 组织基本信息")
@RestController
@RequestMapping("/enterprise/organization")
@Validated
public class OrganizationController {

    @Resource
    private OrganizationService organizationService;
    @Resource
    private ControlPlanService controlPlanService;
    @Resource
    private TenantApi tenantApi;

    @PostMapping("/create")
    @Operation(summary = "创建组织基本信息")
    @PreAuthorize("@ss.hasPermission('enterprise:organization:create')")
    public CommonResult<Long> createOrganization(@Valid @RequestBody OrganizationCreateReqVO createReqVO) {
        return success(organizationService.createOrganization(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新组织基本信息")
    @PreAuthorize("@ss.hasPermission('enterprise:organization:update')")
    public CommonResult<Boolean> updateOrganization(@Valid @RequestBody OrganizationUpdateReqVO updateReqVO) {
        organizationService.updateOrganization(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得组织基本信息")
    @Parameter(name = "planId", description = "计划编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('enterprise:organization:query')")
    public CommonResult<OrganizationRespVO> getOrganization(@RequestParam("planId") Long planId) {
        OrganizationDO organization = organizationService.getOrganizationByPlanId(planId);
        OrganizationRespVO respVO = OrganizationConvert.INSTANCE.convert(organization);
        if (respVO == null) {
            respVO = new OrganizationRespVO();
        }
        // 查询计划信息
        ControlPlanDO controlPlan = controlPlanService.getControlPlan(planId);
        // 查询组织信息
        TenantRespDTO tenant = tenantApi.getTenant(controlPlan.getTenantId()).getCheckedData();
        respVO.setName(tenant.getName())
                .setIndustry(tenant.getIndustry());
        return success(respVO);
    }

}
